import { Ipc_ChannelEnum } from "@enum/channel.enum";
import { ipcRenderer } from "electron";

export class IpcRender {
  static on<T extends FreeArray >(
    key: Ipc_ChannelEnum,
    fn: IpcRenderListener<T>
  ): void {
    ipcRenderer.on(key, fn);
  }

  static once<T extends FreeArray >(
    key: Ipc_ChannelEnum,
    fn: IpcRenderListener<T>
  ): void {
    ipcRenderer.once(key, fn);
  }

  static async invoke<T extends FreeArray , V >(
    key: Ipc_ChannelEnum,
    ...args: T
  ): Promise<V> {
    return await ipcRenderer.invoke(key, ...args);
  }

  static send<T extends FreeArray >(
    key: Ipc_ChannelEnum,
    ...args: T
  ): void {
    ipcRenderer.send(key, ...args);
  }

  static sendSync<T extends FreeArray , V >(
    key: Ipc_ChannelEnum,
    ...args: T
  ): V {
    return ipcRenderer.sendSync(key, ...args);
  }

  static removeListener<T extends FreeArray >(
    key: Ipc_ChannelEnum,
    fn: IpcRenderListener<T>
  ): void {
    ipcRenderer.removeListener(key, fn);
  }

  static removeAllListeners(key: Ipc_ChannelEnum): void {
    ipcRenderer.removeAllListeners(key);
  }

  //向主进程发送port
  static postMessage(
    key: Ipc_ChannelEnum,
    message: Record<string, any>,
    transfer: MessagePort[]
  ): void {
    ipcRenderer.postMessage(key, message, transfer);
  }
}
